package edu.ymu.parm1.action;

import edu.ymu.parm1.model.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
public class PublicAction {

    @RequestMapping("getAnServerString")
    @ResponseBody
    public String returnAnString(){
        return "服务器端返回的字符串";
    }

    //http://localhost:81/getParmValue?sid=201621447112
    @GetMapping("getParmValue")
    @ResponseBody
    public String getParmValue(@RequestParam String sid){
        return sid;
    }

    //http://localhost:81/getPathValue/201621447112
    @GetMapping("getPathValue/{pid}")
    @ResponseBody
    public String getPathValue(@PathVariable(name="pid")String pid){
        return pid;
    }



//提交的参数
//    {
//        "id":"201621447112",
//            "stunum":"12",
//            "name":"马荣志"
//    }
    @PostMapping("receiveAnObject")
    @ResponseBody
    public Student receiveAnObject(@RequestBody Student student){
        return student;
    }


    //form 表单 submit 的参数接收
    @PostMapping("receiveAnFormObject")
    @ResponseBody
    public Student receiveAnFormObject(Student student){
        return student;
    }


    @GetMapping("returnList")
    @ResponseBody
    public List<Student> returnList(){
        //泛型
        List<Student> sl = new ArrayList<Student>();//容器的实例化
        for (int i=0;i<10;i++){
            Student s=new Student();
            s.setId("id"+i);
            s.setStunum("stunum"+i);
            s.setName("name"+i);

            sl.add(s);//放入的容器对象中
        }
        return sl;
    }


    @PostMapping("receiveAnList")
    @ResponseBody
    public List<Student> receiveAnList(@RequestBody List<Student> sl){
        return sl;
    }


}
